<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>取消请求</title>
</head>
<body>
    <button id="btn1">点击发送</button>
    <button id="btn2">点击取消</button>
    <script>

        let btn1 = document.getElementById('btn1')
        let btn2 = document.getElementById('btn2')
        //这个地方要确定的是调用同一个XMLHttpRequest对象，所以要将xhr的作用域提升
        let xhr = null

        btn1.onclick = function () {
            xhr = new XMLHttpRequest()
            xhr.open('GET','http://127.0.0.1:8000/abort')
            xhr.send()
        }


        btn2.onclick = function () {

            xhr.abort()
        }
    </script>
</body>
</html>